package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.SysUser;
import com.itheima.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/user")
public class UserController {

    @Reference
    UserService userService;

    private BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

    /**
     * 登录成功后跳转的页面
     *
     * @return
     */
    @RequestMapping("/doSuccess")
    public String doSuccess() {
        return "redirect:http://localhost:83/pages/main.html";
    }

    /**
     * 登录失败后跳转的页面
     *
     * @return
     */
    @RequestMapping("/doFail")
    public String doFail() {
        return "redirect:http://localhost:83/login.html?message=loginFail";
    }

    /**
     * 页面获取用户名
     */
    @RequestMapping("/getUsername")
    @ResponseBody
    public Result getUsername() {

        try {
            //使用帮助类获取安全框架的上下文对象,然后使用该对象获取授权对象
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

            User user = (User) authentication.getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }

    }


    /**
     * 用户管理-新增用户
     * @param sysUser
     * @return
     */
    @RequestMapping("/save")
    @PreAuthorize("hasAuthority('USER_ADD')")
    @ResponseBody
    public Result save(@RequestBody SysUser sysUser,Integer[] roleIds) {

        try {

            String encode = encoder.encode(sysUser.getPassword());
            sysUser.setPassword(encode);
            userService.save(sysUser,roleIds);
            return new Result(true,MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }
    }

    /**
     * 用户管理-分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    @ResponseBody
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return userService.queryPage(queryPageBean);
    }

    /**
     * 用户管理-编辑信息的回显
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    @ResponseBody
    public Result findById(Integer id) {
        try {
            SysUser sysUser=userService.findById(id);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS,sysUser);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

    /**
     * 用户管理-编辑信息
     * @param sysUser
     * @return
     */
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('USER_EDIT')")
    @ResponseBody
    public Result edit(@RequestBody SysUser sysUser,Integer[] roleIds) {
        try {
            userService.edit(sysUser,roleIds);
            return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAIL);
        }
    }

    /**
     * 用户管理-删除用户
     * @param id
     * @return
     */
    @RequestMapping("/delById")
    @PreAuthorize("hasAuthority('USER_DELETE')")
    @ResponseBody
    public Result delById(Integer id) {
        try {
            userService.delById(id);
            return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
        }catch (RuntimeException e){
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_USER_FAIL);
        }
    }

    @RequestMapping("/findRoleIdsById")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    @ResponseBody
    public Result findRoleIdsById(Integer id){
        try {
            Integer[] roleIds=userService.findRoleIdsById(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
}